﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CPPEI.ETL.Domain.Services
{
    public enum MessageAlertRecordAlertType
    {
        Email = 2,
        SMS = 1
    }
    public enum MessageAlertRecordIsSuccess
    {
        Success = 1,
        Fail = -1,
        SendIn = 0,
        WaitSend = 2


    }

    public class MessageAlertRecordService
    {




        public ServiceResult<Entities.EMessageAlertRecord> Create(string address, int alertType, string content, string title, int isSuccess)
        {
            using (EtlDbContext dbContext = new EtlDbContext())
            {
                EntityRepository<Entities.EMessageAlertRecord> dRep = new EntityRepository<Entities.EMessageAlertRecord>(dbContext);

                Entities.EMessageAlertRecord record = new Entities.EMessageAlertRecord();
                record.Id = Guid.NewGuid();
                record.Address = address;
                record.AlertType = alertType;
                record.Content = content;
                record.Title = title;
                record.CreatedTime = DateTime.Now;
                record.IsSuccess = isSuccess;
                dRep.Create(record);
                if (dbContext.SaveChanges() > 0)
                {
                    return new ServiceResult<Entities.EMessageAlertRecord>(true, ErrorCode.COM_NoError, string.Empty, record);
                }
                else
                {
                    return new ServiceResult<Entities.EMessageAlertRecord>(false, ErrorCode.COM_DBError, string.Empty);
                }
            }
        }
        public PagedList<Entities.EMessageAlertRecord> GetPagedList(string filter, DateTime? begindate, DateTime? enddate, int? state, int? type, string address, int start, int limit)
        {
            using (EtlDbContext dbContext = new EtlDbContext())
            {
                EntityRepository<Entities.EMessageAlertRecord> rep = new EntityRepository<Entities.EMessageAlertRecord>(dbContext);
                List<Entities.EMessageAlertRecord> list = rep.Query().Where(x =>
                (!type.HasValue || x.AlertType == type.Value)
                && (string.IsNullOrEmpty(address) || x.Address.Contains(address))
                && (string.IsNullOrEmpty(filter) || x.Title.Contains(filter))
                && (!state.HasValue || x.IsSuccess == state.Value)
                && (!begindate.HasValue || x.CreatedTime >= begindate)
                && (!enddate.HasValue || x.CreatedTime <= enddate)
                ).Skip(start).Take(limit).OrderByDescending(p => p.CreatedTime).ToList();
                int count = rep.Query().Count(x =>
                (!type.HasValue || x.AlertType == type.Value)
                && (string.IsNullOrEmpty(address) || x.Address.Contains(address))
                && (string.IsNullOrEmpty(filter) || x.Title.Contains(filter))
                && (!state.HasValue || x.IsSuccess == state.Value)
                && (!begindate.HasValue || x.CreatedTime >= begindate)
                && (!enddate.HasValue || x.CreatedTime <= enddate));
                return new PagedList<Entities.EMessageAlertRecord>(true, 0, string.Empty, start, limit, count, list);
            }
        }
    }
}
